home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / star-1_0.tar / starsym.c < prev    next >
C/C++ Source or Header  |  1991-03-22  |  5KB  |  181 lines

  1. /* starsym.c -- STAR-Dependent Symbol Table Operations
  2.  
  3. This file is part of STAR, the Saturn Macro Assembler.
  4.  
  5.    STAR is not distributed by the Free Software Foundation. Do not ask
  6. them for a copy or how to obtain new releases. Instead, send e-mail to
  7. the address below. STAR is merely covered by the GNU General Public
  8. License.
  9.  
  10. Please send your comments, ideas, and bug reports to
  11. Jan Brittenson <bson@ai.mit.edu>
  12.  
  13. */
  14.  
  15.  
  16. /* Copyright (C) 1991 Jan Brittenson.
  17.  
  18.    STAR is free software; you can redistribute it and/or modify it
  19. under the terms of the GNU General Public License as published by the
  20. Free Software Foundation; either version 1, or (at your option) any
  21. later version.
  22.  
  23.    STAR is distributed in the hope that it will be useful, but WITHOUT
  24. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  25. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  26. for more details.
  27.  
  28.    You should have received a copy of the GNU General Public License
  29. along with STAR; see the file COPYING. If not, to obtain a copy, write
  30. to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  31. USA, or send e-mail to bson@ai.mit.edu. */
  32.  
  33. #include <stdio.h>
  34. #include "sects.h"
  35. #include "star.h"
  36. #include "symbols.h"
  37.  
  38.  
  39. SYM_NODE
  40.   *sym_list,            /* `LIST' symbol */
  41.   *sym_stdsect,            /* `STDSECT' symbol */
  42.   *sym_cursect;            /* `SECT' symbol */
  43.  
  44.  
  45. SYM_ROOT
  46.   *symtbl;            /* Global symbol table */
  47.  
  48. int
  49.   nhidden;            /* # of hidden symbols (at end of run) */
  50.  
  51.  
  52. extern char *malloc();
  53. extern void free();
  54.  
  55.  
  56. /* Initialize symbol table */
  57. initsym()
  58.   struct val v;
  59.   extern double version_real;
  60.   extern SECT *stdsect;
  61.   
  62.   
  63.   /* Create symbol table root */
  64.   symtbl = sm_open(malloc, free);
  65.  
  66.   v.type = VT_INT;
  67.   v.vint = 48;
  68.  
  69.   sm_enter_sym(symtbl, "SATURN", v, F_REF+F_HID);
  70.  
  71.   sm_enter_sym(symtbl, "MP", intval(3), F_HID+F_REF);
  72.   sm_enter_sym(symtbl, "SR", intval(2), F_HID+F_REF);
  73.   sm_enter_sym(symtbl, "SB", intval(1), F_HID+F_REF);
  74.   sm_enter_sym(symtbl, "XM", intval(0), F_HID+F_REF);
  75.   sm_enter_sym(symtbl, "VERSION", realval(version_real), F_HID+F_REF);
  76.  
  77.   sm_enter_sym(symtbl, "PI", realval(3.14159265358979323846), F_HID+F_REF);
  78.   sm_enter_sym(symtbl, "LN2", realval(0.69314718055994530942), F_HID+F_REF);
  79.   sm_enter_sym(symtbl, "SQRT2", realval(1.41421356237309504880), F_HID+F_REF);
  80.   sm_enter_sym(symtbl, "E", realval(2.7182818284590452354), F_HID+F_REF);
  81.   sm_enter_sym(symtbl, "LOG2E", realval(1.4426950408889634074), F_HID+F_REF);
  82.   sm_enter_sym(symtbl, "LOG10E", realval(0.43429448190325182765), F_HID+F_REF);
  83.   sm_enter_sym(symtbl, "LN10", realval(2.30258509299404568402), F_HID+F_REF);
  84.  
  85.   sym_list = sm_enter_sym(symtbl, "LIST", intval(1), F_HID+F_REF);
  86.   sym_stdsect = sm_enter_sym(symtbl, "STDSECT", val_zero, F_HID+F_REF);
  87.   sym_cursect = sm_enter_sym(symtbl, "SECT", val_zero, F_HID+F_REF);
  88.  
  89.   sym_cursect->value.type = sym_stdsect->value.type = VT_SECT;
  90.  
  91.   sym_cursect->value.vsect = sym_stdsect->value.vsect= stdsect;
  92.   stdsect->sc_symbol = sym_stdsect;
  93.  
  94.   sm_enter_sym(symtbl, "BITS", intval(sizeof(INT)*8), F_HID+F_REF);
  95. }
  96.  
  97.  
  98. /* Add symbol, as undefined, if not in symbol table */
  99. SYM_NODE *symref(symid)
  100.   char *symid;
  101. {
  102.   SYM_NODE *sym;
  103.  
  104.   /* Simply return it if it already exists */
  105.   if(sym = sm_find_sym(symtbl, symid))
  106.     return(sym);
  107.  
  108.   /* Otherwise create it, but mark as unbound */
  109.   return(sm_enter_sym(symtbl, symid, val_zero, F_UDF));
  110. }
  111.  
  112.  
  113. /* Compare two reference entries for qsort */
  114. static refcmp(r1, r2)
  115.   SYM_NODE **r1, **r2;
  116. {
  117.   extern scmp();
  118.  
  119.   return(scmp((*r1)->name, (*r2)->name));
  120. }
  121.  
  122.  
  123. /* Add symbol tree to reference array */
  124. static void add_sym_to_table(symp, slot, nsymp)
  125.   SYM_NODE *symp, ***slot;
  126.   int *nsymp;
  127. {
  128.   for(; symp; symp = symp->sgt)
  129.     {
  130.       /* Add less-than branch */
  131.       if(symp->slt)
  132.     add_sym_to_table(symp->slt, slot, nsymp);
  133.       
  134.       /* Update statistics */
  135.       if(symp->flags & (F_UDF|F_HID) == F_HID)
  136.     nhidden++;
  137.  
  138.       /* Add this node */
  139.       if(!(symp->flags & (F_UDF | F_HID)) &&
  140.      !(symp->name[0] == 'L' && symp->name[1] == '_'))
  141.     {
  142.       *(*slot)++ = symp;
  143.       (*nsymp)++;
  144.     }
  145.     }
  146. }
  147.  
  148.  
  149. /* Return symbol table reference, suitable for printing */
  150. SYM_NODE **create_sym_ref(nsymp)
  151.   int *nsymp;
  152. {
  153.   SYM_NODE **refarr, **curslot, **curhash;
  154.   extern char *malloc();
  155.  
  156.  
  157.   /* Allocate reference array */
  158.   if(!(refarr = (SYM_NODE **) malloc(symtbl->nsymbols * sizeof(SYM_ROOT **))))
  159.     fatal("Can't allocate %d bytes for symbol table reference",
  160.       symtbl->nsymbols * sizeof(SYM_ROOT));
  161.  
  162.  
  163.   /* Add references tree by tree */
  164.   for(*nsymp = 0,
  165.       curslot = refarr,
  166.       curhash = symtbl->sm_slot,
  167.       nhidden = 0;
  168.  
  169.       curhash < symtbl->sm_slot + SYM_NHASH;
  170.  
  171.       add_sym_to_table(*curhash++, &curslot, nsymp));
  172.  
  173.  
  174.   /* Sort the reference array */
  175.   qsort((char *) refarr, *nsymp, sizeof(SYM_NODE *), refcmp);
  176.  
  177.   /* Return it */
  178.   return(refarr);
  179. }
  180.